<?php

namespace App\Http\Controllers\Home;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;
use DB;

class ServiceController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getIndex(Request $request)
    {
        //获取登录账号id
        $id = $request->session()->get('id');
        if($id){
            $smoke = $this->devInfo('nb_smoke',$id);
            $infrared = $this->devInfo('nb_infrared',$id);
            $gas = $this->devInfo('nb_gas',$id);
            $electric = $this->devInfo('nb_electric',$id);
            $help = $this->devInfo('nb_help',$id);
        }else{
            $smoke = [];
            $infrared = [];
            $gas = [];
            $electric = [];
            $help = [];
        }
//        dd(count($smoke));
        return view('home.service.index',['smoke'=>count($smoke),'infrared'=>count($infrared),'gas'=>count($gas),'electric'=>count($electric),'help'=>count($help)]);
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function getList(Request $request)
    {
        //获取登录账号id
        $id = $request->session()->get('id');
        if($id){
            //获取设备类型
            $type = $request -> input('type');
            switch ($type){
                case 1:
                    $devType = 'smoke';
                    break;
                case 2:
                    $devType = 'infrared';
                    break;
                case 3:
                    $devType = 'electric';
                    break;
                case 4:
                    $devType = 'help';
                    break;
                case 5:
                    $devType = 'gas';
                    break;
                default:
                    $devType = '';
            }
            if($devType){
                $data = $this->devInfo('nb_'.$devType,$id);
            }else{
                $data = [];
            }
        }else{
            $devType = '';
            $data = [];
        }
        return view('home.service.list',['data'=>$data,'devType'=>$devType]);
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        //
    }

    //获取个人所有设备信息
    public function devInfo($dev,$member_id){
        //账号下的烟感设备信息
        $data = DB::table($dev)
            ->leftJoin('room_member','room_member.room_id','=',$dev.'.room_id')
            ->where('room_member.member_id',$member_id)
            ->select('num','verifyCode')
            ->get();
        return $data;
    }
}
